/* Author: 

*/

var fadeInTimeout = 0;
var fadeOutTimeout = 500;

var pages = {
	0:"#page-1",
	1:"#page-2",
	2:"#page-3",
	3:"#page-4",
	4:"#page-5"
};

// utils
var utils = {};

var scrollTop = function(){
	window.scrollTop = 0;
}

// database
utils.db = {};
utils.db.set = function(key, value){
	localStorage.setItem(key, value);
};
utils.db.setPref = function(key, value){
	utils.db.set('preference_'+key, value);
}
utils.db.get = function(key){
	return $.parseJSON( localStorage.getItem(key) );
};
utils.db.getPref = function(key){
	return utils.db.get('preference_'+key);
};

utils.login = function(phonenumber, password){
	
	if ( '' === phonenumber ){
		alert('Empty phonenumber');
		return false;
	}
	if ( '' === password ){
		alert('Empty password');
		return false;
	}  
	
	var _login = utils.db.get('login');
	res = _login !== null && phonenumber === _login.phonenumber && password === _login.password;
	if ( res === false ){
		alert('We are sorry! You have entered a wrong login/password!\nPlease make sure you enter the correct login and password!');
		return false;
	}
	return true;
	
};

utils.view = {};
utils.view.setView = function(view){
	$('.window').not( pages[view] ).fadeOut(fadeOutTimeout, function(){
		$( pages[view] ).show(fadeInTimeout);
	});
};

$(function(){
	
	// set a fake login credentiels
	utils.db.set('login', '{"phonenumber":"0686466378", "password":"12345"}');
	
	// init
	if ( utils.db.getPref('logged') && utils.db.getPref('remember_me') ){
		utils.view.setView(1);
	}
	
	$('a').click(function(e){
		//e.preventDefault();
	});
	$('form').submit(function(e){
		//e.preventDefault();
	});
	
	// menu
	$('a.menu_item').click(function(e){
		
		e.preventDefault();
		
		var id = $(this).attr('href');
		var b = $('#menu').find('.beverage');
		
		$('#menu').find('a.menu_selected').removeClass('menu_selected');
		$(this).addClass('menu_selected');
		
		// show
		$(id).removeClass('hide');
		// hide all but this
		b.not(id).addClass('hide');
		
	});
	
	/*
	$('#login').live('click', function(){
		
		var phonenumber = $('input[name="phone"]');
		var password = $('input[name="password"]');
		
		if ( utils.login(phonenumber.val(), password.val()) ){
			utils.view.setView(1);
			utils.db.setPref('logged', true);
		}
		
	});
	
	$('input[name="remember"]').change(function(){
		utils.db.setPref('remember_me', $(this).is(':checked'));
	}).attr('checked', utils.db.getPref('remember_me'));
	
	$('#logout').live('click', function(){
		utils.view.setView(0);
		utils.db.setPref('logged', false);
	});
		
	$('.back').live('click', function(){
		var index = $('.window').filter(':visible').hide().index();
		$('.window').eq( index-1 ).show(fadeInTimeout);
	});
	
	$('a[href="#edit"]').live('click', function(){
		utils.view.setView(2);
	});
	
	$('a[href="#add-new-selection"]').live('click', function(){
		utils.view.setView(2);
	});
	
	$('#add-new-selection').live('click', function(){
		utils.view.setView(2);
	});
	
	$('a[href="#delete"]').live('click', function(){
		alert('delete');
	});
	*/
	
});





















